package com.miot.domain.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: zhangyanbin
 * @Date: 2019/1/30 14:41
 * @Version 1.0
 */
public class MIHomeUtil {
    protected static Logger logger = LoggerFactory.getLogger(MIHomeUtil.class);

    /**
     * 添加家庭 POST
     */
    public static final String MI_CREATE_HOME = "/bridge/s2s/room/addhome";
    /**
     * 添加房间 POST
     */
    public static final String MI_CREATE_ROOM = "/bridge/s2s/room/addroom";
    /**
     * 修改房间 POST
     */
    public static final String MI_EDIT_ROOM = "/bridge/s2s/room/modify";
    /**
     * 删除房间 POST
     */
    public static final String MI_DELETE_ROOM = "/bridge/s2s/room/delete";
    /**
     * 添加房间设备 POST
     */
    public static final String MI_ADD_DEV_TO_ROOM = "/bridge/s2s/room/adddev";
    /**
     * 删除房间设备 POST
     */
    public static final String MI_DEL_DEV_FROM_ROOM = "/bridge/s2s/room/deldev";
    /**
     * 获取家庭信息 GET
     */
    public static final String MI_GET_ROOM = "/bridge/s2s/room/gethome";


    /**
     * 添加家庭
     *
     * @param controlUnitId
     * @param homeName
     * @return
     */
    public static JSONObject createHome(long controlUnitId, String homeName) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_CREATE_HOME;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("name", homeName);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MIHomeUtil.MI_CREATE_HOME, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->添加家庭接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->添加家庭错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * 获取家庭信息
     *
     * @param controlUnitId
     * @return
     */
    public static JSONObject getHome(long controlUnitId) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_GET_ROOM;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.GET_METHOD, MIHomeUtil.MI_GET_ROOM, dataValueJson.toString());
            String postResult = HttpUtils.sendGet(url, params,null);
            System.err.println(params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->获取家庭信息接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->获取家庭错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * 添加房间
     *
     * @param controlUnitId 用户Id
     * @param homeId        Home的Id, 由2.1addhome接口返回
     * @param roomName      房间名称
     * @return
     */
    public static JSONObject createRoom(Long controlUnitId, Long homeId, String roomName) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_CREATE_ROOM;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("homeId", homeId);
            dataValueJson.put("name", roomName);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MIHomeUtil.MI_CREATE_ROOM, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->添加房间接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->添加房间错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * 修改房间
     *
     * @param controlUnitId 用户Id
     * @param roomId        Home的Id, 由2.2addroom接口返回
     * @param roomName      房间名称
     * @return
     */
    public static JSONObject editRoom(Long controlUnitId, Long roomId, String roomName) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_CREATE_ROOM;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("roomId", roomId);
            dataValueJson.put("name", roomName);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MIHomeUtil.MI_CREATE_ROOM, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->修改房间接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->修改房间错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * 删除房间
     *
     * @param controlUnitId 用户Id
     * @param roomId        Home的Id, 由2.2addroom接口返回
     * @return
     */
    public static JSONObject deleteRoom(Long controlUnitId, Long roomId) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_DELETE_ROOM;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("roomId", roomId);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MIHomeUtil.MI_DELETE_ROOM, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->删除房间接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->删除房间错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * 添加房间设备
     *
     * @param controlUnitId 用户Id
     * @param roomId        Home的Id, 由2.2addroom接口返回
     * @param realDids      房间相关的did列表，是did的JSON数组字符串列表，(这里的did需要调用开放平台接口转换成真正的did)例如 "[\"did1\",\"did2\"]"
     * @return
     */
    public static JSONObject createDevToRoom(Long controlUnitId, Long roomId, String realDids) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_ADD_DEV_TO_ROOM;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("roomid", roomId);
            dataValueJson.put("dids", realDids);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MIHomeUtil.MI_ADD_DEV_TO_ROOM, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->添加房间设备接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->添加房间设备错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }
    /**
     * 删除房间设备
     *
     * @param controlUnitId 用户Id
     * @param roomId        Home的Id, 由2.2addroom接口返回
     * @param realDids      房间相关的did列表，是did的JSON数组字符串列表，(这里的did需要调用开放平台接口转换成真正的did)例如 "[\"did1\",\"did2\"]"
     * @return
     */
    public static JSONObject deleteDevFromRoom(Long controlUnitId, Long roomId, String realDids) {
        try {
            String url = ControlUnitUtil.URI + MIHomeUtil.MI_DEL_DEV_FROM_ROOM;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("roomid", roomId);
            dataValueJson.put("dids", realDids);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MIHomeUtil.MI_DEL_DEV_FROM_ROOM, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = resultJSON.getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->删除房间设备接口返回信息={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口->删除房间设备错误信息={}", e.getMessage());
        }
        return new JSONObject();
    }


}
